{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0dbb29b8",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 日期：2022-10-19 \\week 09(周三）\n",
    "* 课程：python\n",
    "* 记录人：洪梦瑶\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "762f9457",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. 列表的方法\n",
    "> 2. while循环\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a545b405",
   "metadata": {},
   "source": [
    "# 1.列表的方法\n",
    "> 1. 使用Tab键查看列表的方法\n",
    "> 2. 使用help（查看方法的使用规则）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "f44daf70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong']"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"33\",\"xiaohong\"] \n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2def33ae",
   "metadata": {},
   "source": [
    "## 1.1 列表的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "4dcb7b5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "388fa4b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "id": "fa27896c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'Lihua']"
      ]
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed8a5f26",
   "metadata": {},
   "source": [
    "## 1.2 列表的插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "c6678ba1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "id": "aed38376",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'Lihua']"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "5a089bf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"Mike\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "dfaf7ea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Mike', 'Marry', '33', 'xiaohong', 'Lihua']"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "976e88cf",
   "metadata": {},
   "source": [
    "## 1.3 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "91cc6c54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "6b0a5f73",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"Mike\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "id": "f0608137",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'Lihua']"
      ]
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ed80b87",
   "metadata": {},
   "source": [
    "* 正确的删除方法的流程\n",
    "> 1. 查询 value 是否存在列表当中\n",
    "> 2. 对存在的value进行删除\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "d2113df9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hong\" in names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "e450fe6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 合并删除流程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "9b56f6d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"hong\" in names:\n",
    "    names.remove(\"hong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "17f22b37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：删除系统： \n",
    "## 1. 如果输入的值存在，删除； 2. 如果输入的结果不存在，提示用户查询无结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "9c3fedf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'Lihua']"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "id": "bd4d0919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入要删除的内容：5\n",
      "您输入的结果不存在，删除无效\n",
      "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'Lihua']\n"
     ]
    }
   ],
   "source": [
    "user_input_names = input(\"请输入要删除的内容：\") # 输入后直接回车，不要运行；如遇错误可以按上面的中止，然后再次尝试\n",
    "if user_input_names in names:\n",
    "    names.remove(user_input_names)\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"您输入的结果不存在，删除无效\") \n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fa22077",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "ef125c15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新建立names列表数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "id": "5bd14b2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"33\",\"xiaohong\"] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "id": "585909af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong']"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "fb876c0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "63e46ed7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "30b58fdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaohong'"
      ]
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop() # 直接使用pop()，默认删除最后一位."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "8f317893",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33']"
      ]
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "id": "85d408cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 264,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(2) # ()里的2是要删除的object的索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "id": "389bad66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', '33']"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "id": "a187891d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除前查看列表长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "id": "26c3dd3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "id": "d976ca01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入删除的索引值：3\n",
      "您输入的值不在index的范围内\n"
     ]
    }
   ],
   "source": [
    "pop_index = int(input(\"输入删除的索引值：\"))\n",
    "if pop_index<len(names):\n",
    "    names.pop(pop_index)\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"您输入的值不在index的范围内\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "id": "cd6197cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', '33']"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77d24417",
   "metadata": {},
   "source": [
    "# 1.4 列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "id": "9f6f6eb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"33\",\"xiaohong\"] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "id": "e6afb90d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear() # 清空(clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "id": "77025c53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "id": "77823999",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.extend) # 扩展(extend),同list1 + list2这个方法一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "id": "c0ab45f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1 = [\"Jack\",\"Alex\",\"Marry\",\"33\",\"xiaohong\"] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 284,
   "id": "2dc672a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong']"
      ]
     },
     "execution_count": 284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "id": "adc8d3cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aaa', 'bbb', 'ccc']"
      ]
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class2 = [\"aaa\",\"bbb\",\"ccc\"]\n",
    "names_class2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "id": "4641b6f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1.extend(names_class2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "id": "5f1e50ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', '33', 'xiaohong', 'aaa', 'bbb', 'ccc']"
      ]
     },
     "execution_count": 288,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edb3ca61",
   "metadata": {},
   "source": [
    "--------------------------------\n",
    "* sort()排序\n",
    "> 默认升序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "id": "46477d6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生 = [230,250,245,255,260,270]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "id": "e7df2b61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(立定跳远_男生.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "id": "16eb2a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "id": "941731ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[230, 245, 250, 255, 260, 270]"
      ]
     },
     "execution_count": 296,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "id": "d4e0f828",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort(reverse=True) # 降序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "id": "adf80347",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[270, 260, 255, 250, 245, 230]"
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f3fc7fb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "272928c1",
   "metadata": {},
   "source": [
    "# 2. while循环\n",
    "\n",
    "* while True 是循环概念中非常重要的一种方式。它可以实现for循环的指定次数，也可以在特定条件下停止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "id": "ebc9f5e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙 0\n",
      "你是风儿我是沙 1\n",
      "你是风儿我是沙 2\n",
      "你是风儿我是沙 3\n",
      "你是风儿我是沙 4\n",
      "你是风儿我是沙 5\n"
     ]
    }
   ],
   "source": [
    "# 它可以实现for循环的指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙\",count)\n",
    "    count = count+1\n",
    "    if count == 6:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "id": "bb8d48f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:66\n",
      "猜一猜（0-100）：70\n",
      "往小猜！再试一次！\n",
      "猜一猜（0-100）：50\n",
      "往大的猜！\n",
      "猜一猜（0-100）：66\n",
      "猜对了！真棒！\n"
     ]
    }
   ],
   "source": [
    "## 小游戏：猜数字,5次\n",
    "# 1. hide_card : 0-100,大了，小了\n",
    "# 2. guess_digit : 50\n",
    "count = 0\n",
    "hide_card = int(input(\"hide_card:\"))\n",
    "\n",
    "while True:\n",
    "    guess_digit = int(input(\"猜一猜（0-100）：\"))\n",
    "    # 对比:\n",
    "    if hide_card == guess_digit:\n",
    "        print(\"猜对了！真棒！\")\n",
    "        break\n",
    "    elif guess_digit>hide_card:\n",
    "        print(\"往小猜！再试一次！\")\n",
    "    else:\n",
    "        print(\"往大的猜！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd05956",
   "metadata": {},
   "source": [
    "# 课后练习：\n",
    "> 1. 列表的切片和方法的使用\n",
    ">> 1. 课本P67 练习\n",
    ">> 2. 课本P81 练习\n",
    "> 2. 课本P89：列表，我们已经知道什么（总结）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7341479b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
